#pragma once

#include "InteractionFragment.h"

#include "GeneralOrdering.h"
#include "Interaction.h"
#include "InteractionOperand.h"
#include "Lifeline.h"

namespace uml {

void InteractionFragment::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    PartialElement::fromJson(json, errorList);
    JsonSerializable::fromJsonArray(covereds_, "covereds", json, errorList);
    enclosingInteraction_ = JsonSerializable::create<Interaction>(json.value("enclosingInteraction"), errorList);

    enclosingOperand_ = JsonSerializable::create<InteractionOperand>(json.value("enclosingOperand"), errorList);

    JsonSerializable::fromJsonArray(generalOrderings_, "generalOrderings", json, errorList);
}

void InteractionFragment::toJson(QJsonObject& json) {
    PartialElement::toJson(json);
    JsonSerializable::toJsonArray(covereds_, "covereds", json);
    if (enclosingInteraction_) {
        QJsonObject enclosingInteraction_json;
        enclosingInteraction_->toJson(enclosingInteraction_json);
        json.insert("enclosingInteraction", enclosingInteraction_json);
    }
    if (enclosingOperand_) {
        QJsonObject enclosingOperand_json;
        enclosingOperand_->toJson(enclosingOperand_json);
        json.insert("enclosingOperand", enclosingOperand_json);
    }
    JsonSerializable::toJsonArray(generalOrderings_, "generalOrderings", json);
}

/// <summary>
/// covereds getter
/// References the Lifelines that the InteractionFragment involves.
/// </summary>
vector<shared_ptr<Lifeline>>& InteractionFragment::covereds() {
    return covereds_;
}

/// <summary>
/// enclosingInteraction getter
/// The Interaction enclosing this InteractionFragment.
/// </summary>
shared_ptr<Interaction> InteractionFragment::enclosingInteraction() const {
    return enclosingInteraction_;
}

/// <summary>
/// enclosingInteraction setter
/// The Interaction enclosing this InteractionFragment.
/// </summary>
void InteractionFragment::setEnclosingInteraction(shared_ptr<Interaction> value) {
    enclosingInteraction_ = value;
}

/// <summary>
/// enclosingOperand getter
/// The operand enclosing this InteractionFragment (they may nest recursively).
/// </summary>
shared_ptr<InteractionOperand> InteractionFragment::enclosingOperand() const {
    return enclosingOperand_;
}

/// <summary>
/// enclosingOperand setter
/// The operand enclosing this InteractionFragment (they may nest recursively).
/// </summary>
void InteractionFragment::setEnclosingOperand(shared_ptr<InteractionOperand> value) {
    enclosingOperand_ = value;
}

/// <summary>
/// generalOrderings getter
/// The general ordering relationships contained in this fragment.
/// </summary>
vector<shared_ptr<GeneralOrdering>>& InteractionFragment::generalOrderings() {
    return generalOrderings_;
}

} // namespace uml
